package com.examples.gateway.helper.entity;

import com.examples.gateway.common.CheckState;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.server.ServerWebExchange;

import java.util.Map;

@Data
public class RequestContext {

    private static final ThreadLocal<RequestContext> CONTEXT_THREAD_LOCAL = new ThreadLocal<>();

    public static RequestContext currentRequestContext(){
        return CONTEXT_THREAD_LOCAL.get();
    }

    public static void clearRequestContext(){
        CONTEXT_THREAD_LOCAL.remove();
    }

    private CheckRequest request;

    private CheckResponse response;

    /**
     * 属性
     */
    private Map<String, Object> attributes;

    /**
     * exchange对象
     */
    private ServerWebExchange exchange;


    public RequestContext() {

    }

    public RequestContext(ServerWebExchange exchange) {
        this.exchange = exchange;
        request = CheckRequest.newCheckRequest("", exchange.getRequest().getURI().getPath(), exchange.getRequest().getMethod().name());
    }

    public String getRequestUri() {
        return request.uri;
    }

    public void setCheckStatus(CheckState state) {
        if (null == response) {
            response = new CheckResponse();
        }
        response.setState(state);
    }

    public void setResMessage(String message) {
        if (null == response) {
            response = new CheckResponse();
        }
        response.setMessage(message);
    }


    @Getter
    @Setter
    public static class CheckResponse {

        private CheckState state;

        private String message;

    }

}
